package com.onetech.adrewards.free.activities;

import android.content.Intent;
import android.graphics.Color;
import android.os.AsyncTask;
import android.os.AsyncTask.Status;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.google.analytics.tracking.android.EasyTracker;
import com.onetech.adrewards.free.R;
import com.onetech.adrewards.free.config.AppConfig;
import com.onetech.adrewards.free.models.CommonModel;
import com.onetech.adrewards.free.objects.RegisterSuccessDTO;
import com.onetech.adrewards.free.utils.AdrewadsUtils;
import com.todddavies.components.progressbar.ProgressWheel;

public class LoginInviteCodeActivity extends BaseActivity {
	private TextView mMessage1=null;
	private TextView mMessage2=null;
	
	private TextView mInviteNote=null;
	
	private View mRegisterWithCodeBtn=null;
	private View mNormalRegisterBtn=null;
	private ProgressWheel mProgressBar=null;
	
	private long mCurrentTime = 0;
	private boolean isSending=false;
	
	private RegisterInviteCodeDataTask mRegisterInviteDataTask=null;
	RegisterSuccessDTO mResult=null;
	
	private int buttonPressId=-1; 
	
	@Override
	protected void onCreate(Bundle arg0) {
		super.onCreate(arg0);
		setMainView(R.layout.activity_login_invite_code_layout);
		setAppTittle(getString(R.string.app_name));
		showMyPoints(false);
		initView();
		initListener();
	}
	
	private void initView() {
		mMessage1=(TextView)findViewById(R.id.msg1);
		
		
		mInviteNote=(TextView)findViewById(R.id.inviteNote);
		
		mRegisterWithCodeBtn=(LinearLayout)findViewById(R.id.registerInviteCode);
		mNormalRegisterBtn=(LinearLayout)findViewById(R.id.registerNormal);
		
		mProgressBar=(ProgressWheel)findViewById(R.id.splashProgressBar);
		mProgressBar.setText("");
		mProgressBar.setContourColor(Color.TRANSPARENT);
		mProgressBar.spin();
		mProgressBar.setVisibility(View.INVISIBLE);
				
		showFooterView(false);
	}
	
	private void initListener() {
		mRegisterWithCodeBtn.setOnClickListener(this);
		mNormalRegisterBtn.setOnClickListener(this);
	}
	
	@Override
	public void onClick(View v) {
		int id = v.getId();
		switch (id) {	
		case R.id.registerInviteCode:
			
			buttonPressId=R.id.registerInviteCode;
			if(isSending){
				return;
			}
			long mTime=System.currentTimeMillis();
			long time=(mTime-mCurrentTime)/1000;
			if(time > 3){
				mCurrentTime=mTime;
				sendInviteCodeData();				
			}else{
				mCurrentTime=mTime;
				Toast.makeText(getApplicationContext(), getString(R.string.login_invite_code_warning), Toast.LENGTH_LONG).show();
			}
			break;
		case R.id.registerNormal:
			buttonPressId=R.id.registerNormal;
			sendInviteCodeData();
			break;
		default:
			super.onClick(v);
			return;
		}		
	}
	@Override
	protected void onResume() {
		super.onResume();
		if(mResult==null){
			getUserInfo();
		}
	}
	@Override
	public void finish() {
		super.finish();
	}
	private void sendInviteCodeData(){
		if(mRegisterInviteDataTask==null || mRegisterInviteDataTask.getStatus()==Status.FINISHED){
			mRegisterInviteDataTask=new RegisterInviteCodeDataTask();
			mRegisterInviteDataTask.execute();
		}
	}
	
	private class RegisterInviteCodeDataTask extends AsyncTask<Void, Void, RegisterSuccessDTO>{
		@Override
		protected void onPreExecute() {
			super.onPreExecute();
			isSending=true;
			mProgressBar.setVisibility(View.VISIBLE);
		}
		@Override
		protected RegisterSuccessDTO doInBackground(Void... params) {
			try {				
				
				return CommonModel.getInstance().registerInviteCode("");
			} catch (Exception e) {
				return null;
			}			
		}
		@Override
		protected void onPostExecute(RegisterSuccessDTO result) {
			super.onPostExecute(result);
			try {
				if(result!=null){
					mResult=result;
					
					if(result.getExchange_coin_limit()!=null&&result.getExchange_coin_limit().trim().length()>0){
						AppConfig.MIN_COIN_EXCHANGE=Integer.valueOf(result.getExchange_coin_limit());
					}
					
					if(result.getBonus_max_coin()!=null&&result.getBonus_max_coin().trim().length()>0){
						AppConfig.BONUS_MAX_COIN = Integer.valueOf(result.getBonus_max_coin());
					}
					
					if(result.getLogin_bonus_flag().trim().equals("0")){
						AppConfig.LOGIN_BONUS=false;
					}else{
						AppConfig.LOGIN_BONUS=true;
					}
					
					if(result.getAd_sort() !=null && result.getAd_sort().trim().length()>0){
						AppConfig.AD_SORT = Integer.valueOf(result.getAd_sort());
					}
					
					
					if(buttonPressId==R.id.registerInviteCode){
						if(result.getError_code().trim().equals("002-006") ){
							AdrewadsUtils.showMessage(getApplicationContext(), result.getError_message());
							return;
						}else{
							
							go2LoginInviteCodeSendMail();
						}
					}else{
						
						go2HomePage();
					}
				}else{
					AdrewadsUtils.showInfoDialog(getApplicationContext(), getString(R.string.error_message_title), getString(R.string.server_error_message));
				}
			} catch (Exception e) {
				AdrewadsUtils.showInfoDialog(getApplicationContext(), getString(R.string.error_message_title), getString(R.string.server_error_message));
			}
			finally{
				mProgressBar.setVisibility(View.INVISIBLE);
				isSending=false;
			}
		}
	}
	
	
	
	public void go2HomePage(){
		try {
			Intent intent=new Intent(getApplicationContext(), BaseNewActivity.class);
			intent.putExtra("m_id", "2835");
			intent.putExtra("m_owner_id", "558");
			if(mResult!=null && mResult.getMem_id()!=null && mResult.getMem_id().trim().length()>0){
				AppConfig.uId=mResult.getMem_id().trim();
				intent.putExtra("user_id", mResult.getMem_id().trim());
				intent.putExtra("uid", mResult.getMem_id().trim());
			}else{
				intent.putExtra("user_id", "383cb3f413cc96b69cc78b818e433748");//account test
				intent.putExtra("uid", "383cb3f413cc96b69cc78b818e433748");//account test
			}
			intent.putExtra("platform_id", "0");
			intent.putExtra("url", "http://car.mobadme.jp/spg/spnew/558/2835/index.php");
			intent.putExtra("api_key", "76552d4faa4dd35b");
			intent.putExtra("app_key", "ncIdX3la");
			intent.putExtra("loading_msg", "loading..");
			intent.putExtra("timeout", "3000");

			////
			
			intent.putExtra("appkey", "ncIdX3la");
			////

			intent.putExtra("show_button", true);
			intent.putExtra("button_string", "back");
			intent.putExtra("bk_color", Color.BLUE);
			intent.putExtra("button_gravity", Gravity.RIGHT);
			
			startActivity(intent);
			finish();
		} catch (Exception e) {
			return;
		}
	}
	
	
	public void go2LoginInviteCodeSendMail(){
		try {
			Intent intent=new Intent(getApplicationContext(), LoginInviteCodeSendMailActivity.class);
			
			if(mResult!=null && mResult.getMem_id()!=null && mResult.getMem_id().trim().length()>0){
				AppConfig.uId=mResult.getMem_id().trim();
				intent.putExtra("user_id", mResult.getMem_id().trim());
				intent.putExtra("uid", mResult.getMem_id().trim());
			}
			startActivity(intent);
			finish();
		} catch (Exception e) {
			return;
		}
	}
	
	
	@Override
	public void onStart() {
	    super.onStart();
	    try {
	    	EasyTracker.getInstance(this).activityStart(this);  // Add this method.
		} catch (Exception e) {			
		}
	  }

	  @Override
	  public void onStop() {
	    super.onStop();
	    try {
	    	EasyTracker.getInstance(this).activityStop(this);  // Add this method.
		} catch (Exception e) {
		}
	  }	 
}
